from datetime import datetime, time

from sqlalchemy import delete, func, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_basics.entity.do.thesaurus_do import ComparativeProfessionalThesaurus, ComparativeProfessionalData
from module_basics.entity.vo.thesaurus_vo import ThesaurusQueryModel, ThesaurusDataQueryModel
from utils.page_util import PageUtil


class ThesaurusDao:
    @classmethod
    async def get_thesaurus_list(cls, db: AsyncSession, query_object: ThesaurusQueryModel, is_page: bool = False):
        """
        根据查询参数获取岗位列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 岗位列表信息对象
        """
        query = (
            select(ComparativeProfessionalThesaurus)
            .where(
                ComparativeProfessionalThesaurus.thesaurus_name.like(
                    f'%{query_object.thesaurus_name}%') if query_object.thesaurus_name else True,
                ComparativeProfessionalThesaurus.thesaurus_type.like(
                    f'%{query_object.thesaurus_type}%') if query_object.thesaurus_type else True,
                ComparativeProfessionalThesaurus.status == query_object.status if query_object.status else True,
                ComparativeProfessionalThesaurus.create_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_time and query_object.end_time
                else True,
            )
        )
        thesaurus_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return thesaurus_list

    @classmethod
    async def get_thesaurus_catalogue_detail_by_info(cls, db, thesaurus_catalogue):
        """
        根据字典类型参数获取字典类型信息

        :param db: orm对象
        :param thesaurus_catalogue: 字典类型参数对象
        :return: 字典类型信息对象
        """
        thesaurus_catalogue_info = (
            (
                await db.execute(
                    select(ComparativeProfessionalThesaurus).where(
                        ComparativeProfessionalThesaurus.thesaurus_type == thesaurus_catalogue.thesaurus_type if thesaurus_catalogue.thesaurus_type else True,
                        ComparativeProfessionalThesaurus.thesaurus_type == thesaurus_catalogue.thesaurus_name if thesaurus_catalogue.thesaurus_name else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return thesaurus_catalogue_info

    @classmethod
    async def add_thesaurus_catalogue_dao(cls, db, thesaurus_catalogue):
        """
         新增字典类型数据库操作

         :param db: orm对象
         :param thesaurus_catalogue: 字典类型对象
         :return:
         """
        db_thesaurus_catalogue = ComparativeProfessionalThesaurus(**thesaurus_catalogue.model_dump())
        db.add(db_thesaurus_catalogue)
        await db.flush()

        return db_thesaurus_catalogue

    @classmethod
    async def get_thesaurus_catalogue_detail_by_id(cls, db, thesaurus_id):
        """
       根据字典类型id获取字典类型详细信息

       :param db: orm对象
       :param thesaurus_id: 字典类型id
       :return: 字典类型信息对象
       """
        thesaurus_catalogue_info = (await db.execute(select(ComparativeProfessionalThesaurus).where(
            ComparativeProfessionalThesaurus.thesaurus_id == thesaurus_id))).scalars().first()

        return thesaurus_catalogue_info

    @classmethod
    async def edit_thesaurus_catalogue_dao(cls, db, edit_thesaurus_catalogue):
        """
        编辑字典类型数据库操作

        :param db: orm对象
        :param edit_thesaurus_catalogue: 需要更新的字典类型字典
        :return:
        """
        await db.execute(update(ComparativeProfessionalThesaurus), [edit_thesaurus_catalogue])

    @classmethod
    async def delete_thesaurus_catalogue_dao(cls, db, param):
        """
        删除专业词库数据库操作

        :param db: orm对象
        :param param: 字典类型对象
        :return:
        """
        await db.execute(delete(ComparativeProfessionalThesaurus).where(
            ComparativeProfessionalThesaurus.thesaurus_id.in_([param.thesaurus_id])))


class ThesaurusDataDao:

    @classmethod
    async def get_thesaurus_data_list(cls, db: AsyncSession, query_object: ThesaurusDataQueryModel,
                                      is_page: bool = False):
        """
        根据查询参数获取字典数据列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 字典数据列表信息对象
        """
        query = (
            select(ComparativeProfessionalData)
            .where(
                ComparativeProfessionalData.thesaurus_type == query_object.thesaurus_type
                if query_object.thesaurus_type else True,
                ComparativeProfessionalData.status == query_object.status if query_object.status else True,
            )
            .order_by(ComparativeProfessionalData.data_sort)
            .distinct()
        )
        dict_data_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return dict_data_list

    @classmethod
    async def edit_thesaurus_data_dao(cls, db: AsyncSession, thesaurus_data: dict):
        """
        编辑专业词库数据数据库操作

        :param db: orm对象
        :param thesaurus_data: 需要更新的字典数据字典
        :return:
        """
        await db.execute(update(ComparativeProfessionalData), [thesaurus_data])

    @classmethod
    async def count_thesaurus_data_dao(cls, db, thesaurus_type):
        """
        根据字典类型查询字典类型关联的字典数据数量

        :param db: orm对象
        :param thesaurus_type: 字典类型
        :return: 字典类型关联的字典数据数量
        """
        dict_data_count = (
            await db.execute(select(func.count('*')).select_from(ComparativeProfessionalData).where(
                ComparativeProfessionalData.thesaurus_type == thesaurus_type))
        ).scalar()

        return dict_data_count

    @classmethod
    async def get_thesaurus_data_detail_by_info(cls, db, thesaurus_data):
        """
       根据字典数据参数获取字典数据信息

       :param db: orm对象
       :param thesaurus_data: 字典数据参数对象
       :return: 字典数据信息对象
       """
        thesaurus_data_info = (
            (
                await db.execute(
                    select(ComparativeProfessionalData).where(
                        ComparativeProfessionalData.dict_type == thesaurus_data.dict_type,
                        ComparativeProfessionalData.data_name == thesaurus_data.data_name,
                    )
                )
            )
            .scalars()
            .first()
        )

        return thesaurus_data_info

    @classmethod
    async def add_thesaurus_data_dao(cls, db, thesaurus_data):
        """
        新增字典数据数据库操作

        :param db: orm对象
        :param thesaurus_data: 字典数据对象
        :return:
        """
        db_data_type = ComparativeProfessionalData(**thesaurus_data.model_dump())
        db.add(db_data_type)
        await db.flush()

        return db_data_type

    @classmethod
    async def get_thesaurus_data_detail_by_id(cls, db, data_id):
        """
        根据字典数据id获取字典数据详细信息

        :param db: orm对象
        :param data_id: 字典数据id
        :return: 字典数据信息对象
        """
        dict_data_info = (
            (await db.execute(select(ComparativeProfessionalData).where(
                ComparativeProfessionalData.data_id == data_id))).scalars().first()
        )

    @classmethod
    async def delete_thesaurus_data_dao(cls, db, param):
        """
        删除专业词酷数据数据库操作

        :param db: orm对象
        :param param: 字典数据对象
        :return:
        """
        await db.execute(
            delete(ComparativeProfessionalData).where(ComparativeProfessionalData.data_id.in_([param.data_id])))
